package com.acce.gxsc.page.inCarDetail;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.acce.gxsc.R;
import com.acce.gxsc.common.Constants;
import com.acce.gxsc.common.DateUtils;
import com.acce.gxsc.common.QuizActivity;
import com.acce.gxsc.common.Tools;
import com.acce.gxsc.model.netBean.InCarBean;
import com.acce.gxsc.model.netBean.InCarGoodsBean;
import com.acce.gxsc.page.inCarDetail.editInCarDetail.EditInCarDetailActivity;
import com.acce.sdk.User;
import com.acce.sdk.base.mvp.BaseMvpActivity;
import com.acce.sdk.utils.EncodingHandler;
import com.acce.sdk.utils.StringUtils;
import com.acce.sdk.utils.ToastUtils;

import java.text.MessageFormat;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.acce.gxsc.common.Constants.ID_1;

/**
 * Created by huxley on 2017/5/26.
 */
public class InCarActivity extends BaseMvpActivity<RegisterCarView, RegisterCarPresenter> implements RegisterCarView {

    LayoutInflater mInflater;
    @Bind(R.id.tv_order_id)
    TextView     mTvOrderId;
    @Bind(R.id.tv_father)
    TextView     mTvFather;
    @Bind(R.id.tv_son)
    TextView     mTvSon;
    @Bind(R.id.tv_customer)
    TextView     mTvCustomer;
    @Bind(R.id.tv_car_info)
    TextView     mTvCarInfo;
    @Bind(R.id.tv_box_info)
    TextView     mTvBoxInfo;
    @Bind(R.id.ll_goods_list)
    LinearLayout mLlGoodsList;
    @Bind(R.id.tv_goods_info)
    TextView     mTvGoodsInfo;
    @Bind(R.id.btn_delete)
    TextView btnEditInCar;

    InCarBean                 inCarInfo;
    ArrayList<InCarGoodsBean> inGoodsInfo;
    boolean                   isSelect;
    @Bind(R.id.tv_acce_temp_1)
    TextView mTvAcceTemp1;


    @Override
    protected RegisterCarPresenter createPresenter() {
        return new RegisterCarPresenter();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_in_car);
        ButterKnife.bind(this);
        init();
        String inCarKey = getIntent().getStringExtra("inCarKey");
        mPresenter.loadInCarDetail(inCarKey);
    }

    private void init() {
        if(User.getInstance().getCompanyAuthority().equals("保安队长")){
            btnEditInCar.setVisibility(View.VISIBLE);
        }
        mInflater = LayoutInflater.from(this);
        if (getIntent().hasExtra(Constants.Key.IS_SELECT)) {
            isSelect = getIntent().getBooleanExtra(Constants.Key.IS_SELECT, false);
        }
    }

    @Override
    public void printSuccess() {
        if (isSelect) {
            Intent registerCarIntent = new Intent(this, QuizActivity.class);
            registerCarIntent.putExtra(Constants.Key.CATEGORY_ID, ID_1);
            startActivity(registerCarIntent);
        }
    }

    @Override
    public void loadInCarDetail(InCarBean inCarInfo, ArrayList<InCarGoodsBean> inGoodsInfo) {
        this.inCarInfo = inCarInfo;
        this.inGoodsInfo = inGoodsInfo;
        Tools.setText(mTvOrderId, inCarInfo.inCarId);
        Tools.setText(mTvFather, inCarInfo.underCompany);
        Tools.setText(mTvSon, inCarInfo.consignor);
        Tools.setText(mTvCustomer, inCarInfo.contactName);
        Tools.setText(mTvBoxInfo, inCarInfo.containNo);
        mTvCarInfo.setText(Tools.getString1(inCarInfo.plateNumX, inCarInfo.plateNum, inCarInfo.hangNum));
        mTvAcceTemp1.setText(Tools.getString(inCarInfo.acceTemp1));
        Tools.setText(mTvGoodsInfo, MessageFormat.format("-{0}-项", inGoodsInfo == null ? 0 : inGoodsInfo.size()));
        mLlGoodsList.removeAllViews();
        if (inGoodsInfo != null) {
            for (int i = 0; i < inGoodsInfo.size(); i++) {
                InCarGoodsBean goodsBean = inGoodsInfo.get(i);
                View childView = mInflater.inflate(R.layout.child_goods, mLlGoodsList, false);
                TextView tvGoodsId = (TextView) childView.findViewById(R.id.tv_goods_id);
                TextView tvGoodsType = (TextView) childView.findViewById(R.id.tv_goods_type);
                TextView tvGoodsName = (TextView) childView.findViewById(R.id.tv_goods_name);
                TextView tvGoodsPackaging = (TextView) childView.findViewById(R.id.tv_packaging);
                TextView tvGoodsNum1 = (TextView) childView.findViewById(R.id.tv_goods_num_1);
                TextView tvGoodsNum2 = (TextView) childView.findViewById(R.id.tv_goods_num_2);
                TextView tvWeight = (TextView) childView.findViewById(R.id.tv_weight);
//                TextView tvTotalWeight = (TextView) childView.findViewById(R.id.tv_total_weight);
                tvGoodsId.setText(String.valueOf(i + 1));
                tvGoodsType.setText(goodsBean.goodsType);
                tvGoodsName.setText(goodsBean.goodsName);
                tvGoodsPackaging.setText(goodsBean.containType);
                tvGoodsNum1.setText(goodsBean.plateAmount);
                tvGoodsNum2.setText(goodsBean.amount);
                tvWeight.setText(goodsBean.itemWeight);
//                tvTotalWeight.setText(goodsBean.suttle);
                mLlGoodsList.addView(childView);
            }
        }
    }

    @Override
    public void deleteOrderSuccess() {
        finish();
    }

    @OnClick({R.id.btn_in,R.id.btn_delete})
    public void onClick(View view) {
        if (StringUtils.isEmpty(inCarInfo.inCarKey)) {
            ToastUtils.show("未找到该订单！");
            return;
        }
        switch (view.getId()) {
            case R.id.btn_in:
                ScrollView rootView = (ScrollView) mInflater.inflate(R.layout.dialog_in_car_info, (ViewGroup) findViewById(R.id.dialog));
                final LinearLayout llContent = ButterKnife.findById(rootView, R.id.ll_content);
                TextView tvDiliveryType = ButterKnife.findById(rootView, R.id.tv_dilivery_type);
                TextView tvTime = ButterKnife.findById(rootView, R.id.tv_time);
                TextView tvCompany = ButterKnife.findById(rootView, R.id.tv_company);
                TextView tvCustomer = ButterKnife.findById(rootView, R.id.tv_customer);
                TextView tvCarInfo = ButterKnife.findById(rootView, R.id.tv_car_info);
                TextView tvBoxInfo = ButterKnife.findById(rootView, R.id.tv_box_info);
                TextView tvHangNum = ButterKnife.findById(rootView, R.id.tv_hang_num);
                TextView tvGoodsInfo = ButterKnife.findById(rootView, R.id.tv_goods_info);
                final ImageView ivBarCode = ButterKnife.findById(rootView, R.id.iv_bar_code);
                TextView tvId = ButterKnife.findById(rootView, R.id.tv_id);
                TextView tvTollCollector = ButterKnife.findById(rootView, R.id.tv_toll_collector);
                TextView tvDate = ButterKnife.findById(rootView, R.id.tv_date);
                tvTime.setText(DateUtils.toFormat(inCarInfo.createTime));
                tvHangNum.setText(Tools.getString(inCarInfo.hangNum));

                tvCompany.setText(Tools.getString1(inCarInfo.consignor, inCarInfo.underCompany));
                Tools.setText(tvCustomer, inCarInfo.contactName);
                Tools.setText(tvDiliveryType, Tools.getString(inCarInfo.acceTemp1));
                tvCarInfo.setText(Tools.getString1(inCarInfo.plateNumX, inCarInfo.plateNum));
                Tools.setText(tvBoxInfo, inCarInfo.containNo);
//        tvCarInfo.setText(Tools.getString1(inCarInfo.plateNumX, inCarInfo.plateNumG));
//        tvBoxInfo.setText(Tools.getString2(inCarInfo.preContainNo, inCarInfo.preContainSize));
                Tools.setText(tvGoodsInfo, inCarInfo.goodsInfo);
//        final String id = MessageFormat.format("CAR-{0}-{1}", App.dutyId, inCarInfo.inCarId);
                tvId.setText(inCarInfo.inCarId);
                tvTollCollector.setText(MessageFormat.format("操作员：{0}", User.getInstance().getRealName()));
                tvDate.setText(DateUtils.toFormat(inCarInfo.createTime));
                getWindow().getDecorView().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Bitmap codeImage = null;
                        try {
                            codeImage = EncodingHandler.createBarCode128(inCarInfo.inCarId, (int) (llContent.getWidth() * 1.5), (int) getResources().getDimension(R.dimen.x200));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (codeImage != null) {
                            ivBarCode.setImageBitmap(codeImage);
                        }
                    }
                }, 300);
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
                final AlertDialog alertDialog = builder
                        .setTitle("是否提交进场单")
                        .setView(rootView)
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                mPresenter.printOrder(llContent);
                            }
                        }).create();
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();
                break;

            case R.id.btn_delete:
                Intent intent=new Intent(this, EditInCarDetailActivity.class);
                intent.putExtra("inCarInfo",inCarInfo);
                intent.putExtra("inGoodsInfo",inGoodsInfo);
                startActivity(intent);
                break;
        }
    }
}
